package com.jzmy.cultivate.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzmy.cultivate.dao.TrainingResourcesDao;
import com.jzmy.cultivate.entity.TrainingResources;
import com.jzmy.cultivate.service.TrainingResourcesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 培训资源表
 * @author mengzhichao
 * @create 2022-08-25-17:53
 */
@Service
public class TrainingResourcesServiceImpl extends ServiceImpl<TrainingResourcesDao, TrainingResources> implements TrainingResourcesService {
    @Override
    public List<TrainingResources> findByKv(Object... params) {
        if (params == null) {
            return new ArrayList<>();
        }
        return baseMapper.selectByMap(convertToMap(params));
    }

    public Map<String, Object> convertToMap(Object... params) {
        Map<String, Object> map = new HashMap<>();
        if (params == null) {
            return map;
        }
        for (int i = 0; i < params.length; i++) {
            if (i % 2 == 1 && params[i] != null && StringUtils.isNotBlank(params[i].toString())) {
                map.put((String) params[i - 1], params[i]);
            }
        }
        return map;
    }
}
